﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Net.Mail;
using System.Net.Mime;
using System.Net;

namespace Mime_Mail_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string htmltxt = "<html><body><img src=\"cid:image1 \" /></body></html>";
                MailMessage mailMessage = new MailMessage();

                mailMessage.From = new MailAddress(textBoxFrom.Text);
                mailMessage.To.Add(new MailAddress(textBoxTo.Text));
                mailMessage.Subject = "test image embed";

                LinkedResource photo = new LinkedResource(openFileDialog1.FileName);
                photo.ContentId = "image1";
                AlternateView av = AlternateView.CreateAlternateViewFromString(htmltxt, null, MediaTypeNames.Text.Html);
                mailMessage.AlternateViews.Add(av);
                av.LinkedResources.Add(photo);
                mailMessage.IsBodyHtml = true;

                NetworkCredential credentials = new NetworkCredential(textBoxFrom.Text, textBoxPassword.Text);
                SmtpClient mailSender = new SmtpClient("smtp.gmail.com", 587);
                mailSender.EnableSsl = true;
                mailSender.Credentials = credentials;
                try
                {
                    mailSender.Send(mailMessage);

                    MessageBox.Show("E-mail sent");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failure: " + ex.ToString());
                }
            }

        }
    }
}
